<?php

namespace App\Http\Controllers\Admin;

//use App\Http\Requests\StoreArticleValidate;
use App\Models\Article;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class ArticleController extends BaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        if ($request->header('X-Requested-With') == 'XMLHttpRequest') {
            //检索
            //搜索关键词
            $title = $request->get('title');
            //起止时间
            $startTime = $request->get('starttime');
            $endTime = $request->get('endtime');
            //排序
            $order = $request->get('order');
            $data = $request->get('columns');
            //排序的字段
            $fields = $data[$order[0]['column']]['data'];
            //排序的类型
            $sort = $order[0]['dir'];
            //分页
            //开始位置
            $start = $request->get('start');
            $query = Article::where('id','>',0);
            if (!empty($startTime) && !empty($endTime)) {
                $query->whereBetween('created_at',[$startTime,$endTime]);
            }
            if (!empty($title)){
                $query->where('title','like',"%{$title}%");
            }
            //获取记录数
            $length = min(100, $request->get('length', 10));
            //记录总数
            $total = $query->count();
            //获取数据
            $data = $query->offset($start)->limit($length)->orderBy($fields, $sort)->get();
            $result = [
                'draw' => $request->get('draw'),
                'recordsTotal' => $total,
                'recordsFiltered' => $total,
                'data' => $data
            ];
            return $result;
        }
        return view('admin.article.index');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        return view('admin.article.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $res['pic'] = config('up.pic');
        $res = $request->except('_token', 'url');
        if ($request->hasFile('pic')) {
            $pic = $request->pic->store('','article');
            $res['pic'] = '/uploads/article/'.date("Ymd").'/'.$pic;
        }
        $res = Article::create($res);
        if ($res) {
            return redirect(route('admin.article.index'))->withErrors(['error', '新增失败!']);
        }
        return redirect(route('admin.article.index'))->with(['success' => '新增成功!']);
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Article  $article
     * @return \Illuminate\Http\Response
     */
    public function show(Article $article)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\Article  $article
     * @return \Illuminate\Http\Response
     */
    public function edit(Article $article)
    {
//        $item = Article::find($article->id)->toArray();
        return view('admin.article.edit',compact('article'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Article  $article
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Article $article)
    {
        $data = $request->except('_token','url','_method');
        if (!$data['picPre']){
            $data['pic'] = config('up.pic');
        }
        if ($request->hasFile('pic')) {
            $pic = $request->pic->store('','article');
            $data['pic'] = '/uploads/article/'.date("Ymd").'/'.$pic;
        }
        unset($data['picPre']);
        $res = $article->update($data);
        if ($res) {
            return redirect(route('admin.article.index'))->withErrors(['error', '修改失败!']);
        }
        return redirect(route('admin.article.index'))->with(['success' => '修改成功!']);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Article  $article
     * @return \Illuminate\Http\Response
     */
    public function destroy(Article $article)
    {
        $res = $article->delete();
        if ($res) {
            $this->success();
        } else {
            $this->fail();
        }
    }
}
